import React from 'react'
import { connect } from 'dva'
import { Card} from 'antd'
import moment from 'moment'

/**
 * HoldingTable 其他持仓
 */
import TableList from '../../../components/TableList'
class HoldingTable extends React.Component {
    state = {
        data: this.props.data,
    }
    componentDidMount() {
        this.fetchData(this.props)
    }
    componentWillReceiveProps(nextProps) {

        if ((nextProps.productId !== this.props.productId) || (nextProps.date !== this.props.date)) {
            this.fetchData(nextProps)
        }
        if (nextProps.data !== this.props.data) {
            nextProps.data.fieldList=nextProps.data.field

            

            if (nextProps.data.fieldList&&nextProps.data.fieldList.length) {
                
                // const ratioArr = nextProps.data.fieldList.filter(item => (item.name.indexOf('比例')>-1)||(item.name.indexOf('收益率')>-1))
                // const dateArr = nextProps.data.fieldList.filter(item => (item.name.indexOf('日期')>-1)||(item.name.indexOf('时间')>-1))
                const ratioArr = nextProps.data.fieldList.filter(item => (item.name.indexOf('比例')>-1)||(item.name.indexOf('收益率')>-1))
                const dateArr = nextProps.data.fieldList.filter(item => (item.name.indexOf('日期')>-1)||(item.name.indexOf('时间')>-1))
                const stockArr = nextProps.data.fieldList.filter(item => (item.name.indexOf('建仓成本')>-1))
                nextProps.data.list.map(item => {
                    // ratioArr.map(it=>{
                    //     item[it.id] = item[it.id]&&item[it.id].toPercent()
                    // })
                    // dateArr.map(it=>{
                    //     item[it.id] = item[it.id]&&moment(item[it.id]).format('YYYY-MM-DD')
                    // })
                    ratioArr.map(it=>{
                        item[it.id] = item[it.id]&&item[it.id].toPercent()
                    })
                    dateArr.map(it=>{
                        item[it.id] = item[it.id]&&moment(item[it.id]).format('YYYY-MM-DD')
                    })
                    stockArr.map(it=>{
                        item[it.id] = item[it.id]&&Math.round(item[it.id]*100)/100
                    })
                    
                })
                console.log(119,nextProps.data.list);
                
            }
            this.setState({
                data: nextProps.data,            
            })
        }
    }
    fetchData = (props) => {
        eval(`
        props.dispatch({
            type: 'combinatorialAnalysis/get${props.tableType}',
            payload:props
        })
        `)
    }
    render() {
        let title
        if(this.props.title){
            title=<strong>{this.props.title}{this.state.data.list?<span style={{marginLeft:10}}>({this.state.data.list.length})</span>:''}</strong>
        }
        return (
            <Card title={title||false} bordered={false} className="tableCard" >
            {
                this.state.data&&<TableList {...this.state.data} rowKey='code' pagination={this.state.data.list&&this.state.data.list.length>10?{ size:"small"}:false} />
            }
            </Card>

        )
    }
}

HoldingTable.defaultProps = {
    productId: null,
    date: null,
    title:'',
    tableType:'OtherHolding',
    data: {
        // "field": [{
        //     "id": "code",
        //     "name": "股票代码"
        // },
        // {
        //     "id": "name",
        //     "name": "股票简称"
        // },
        // {
        //     "id": "days",
        //     "name": "持仓天数"
        // },
        // {
        //     "id": "weight",
        //     "name": "平均持股权重"
        // },
        // {
        //     "id": "ratio",
        //     "name": "收益贡献占比"
        // }],
        // "list": [{
        //     "id":"1",
        //     "code": "600000-CN",
        //     "name": "浦发银行",
        //     "days": 270,
        //     "weight": 0.1178,
        //     "ratio": 6.33
        // }]
    }
}

export default connect()(HoldingTable)
